﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TMS
{
    public partial class CreateEditPartnerControl : UserControl
    {
        bool edit = false;
        int ID = 0;

        public CreateEditPartnerControl()
        {
            Program.GetMainForm.Text = "TMS - Нов коминтент";
            InitializeComponent();
        }

        public CreateEditPartnerControl(DataGridViewRow parameters)
        {
            InitializeComponent();
            Program.GetMainForm.Text = "TMS - Коминтент: " + parameters.Cells[1].Value.ToString(); ;
            edit = true;

            txtPartnerName.Text = parameters.Cells[1].Value.ToString();
            txtPartnerAddress.Text = parameters.Cells[2].Value.ToString();
            txtPostalNum.Text = parameters.Cells[3].Value.ToString();
            txtCity.Text = parameters.Cells[4].Value.ToString();
            txtPartnerTelephone.Text = parameters.Cells[5].Value.ToString();
            txtTaxNumber.Text = parameters.Cells[6].Value.ToString();
            txtCountry.Text = parameters.Cells[7].Value.ToString();
            txtEmail.Text = parameters.Cells[8].Value.ToString();
            txtContact.Text = parameters.Cells[9].Value.ToString();

            ID = Convert.ToInt32(parameters.Cells[0].Value);
            
        }

       

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape) this.Dispose();
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void ToStSave_Click(object sender, EventArgs e)
        {
            Partner partner = new Partner(ID, txtPartnerName.Text, txtPartnerAddress.Text, txtPostalNum.Text, txtCity.Text, txtPartnerTelephone.Text, txtTaxNumber.Text, txtCountry.Text, txtEmail.Text, txtContact.Text);

            if (edit)
            {
                partner.UpdatePartnerInDB();
            }
            else
            {
                if (partner.CheckExistingPartnerInDB())
                {
                    MessageBox.Show("Веќе постои кореспондент со исто име или даночен број!");
                }
                else
                    partner.InsertPartnerInDB();
            }

            this.Hide();
            Program.GetMainForm.ShowControl(ControlsEnum.PARTNERS_CONTROL);
        }

        private void ToStDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Сигурни сте дека сакате да го избришете кореспондентот?", "Потврда за бришење", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Partner partner = new Partner(ID);
                partner.DeletePartnerFromDB();

                Program.GetMainForm.ShowControl(ControlsEnum.PARTNERS_CONTROL);
                this.Dispose();
            }
        }

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }


    }
}
